<?php
/**
 * Create by 11li
 * Date 2020/1/16
 * Time 8:46
 *
 */

namespace app\api\controller\v3;


use app\admin\model\User;
use app\common\controller\Api;
use app\common\model\AgentShop;
use app\common\model\Commission;
use app\common\model\Order;
use app\common\service\AgentService;
use app\common\service\CommissionService;
use app\common\service\ShipOrderService;
use app\common\service\UserService;
use app\common\service\v2\OrderService;
use think\Db;
use think\Exception;
use think\Request;

class Center extends Api
{
	
	protected $agent;
	
	public function __construct(Request $request = null)
	{
		parent::__construct($request);
		$userId = $this->auth->id;
		$agentId = UserService::getUserAgentId($userId);
		if(!$agentId) {
			$this->error('权限不足');
		}
		
		$agent = AgentShop::get($agentId);
		if($agent['role'] != User::LEVEL_CENTER) {
			$this->error('权限不足');
		}
		
		$this->agent = $agent;
	}
	
	/**
	 * 统计
	 */
	public function statistics() {
		$userId = $this->auth->id;
		$agent = $this->agent;
		$agentId = $agent['id'];
		
		$subAgentIds = AgentService::getAgentSubIds($agentId, 'center_id');
		//代理ID集合
		$subAgentIds[] = $agentId;
		//代理用户ID集合
		$subAgentUserIds = array_merge([$userId], array_keys($subAgentIds));
		
		//代言人ID集合
		$promotionIds = AgentService::getAgentSubPromotionIds($subAgentIds);
		//代言人用户ID集合
		$promotionUserIds = array_keys($promotionIds);
		
		$userIds = array_merge($subAgentUserIds, $promotionUserIds);
		//粉丝
		$fansNumber = UserService::getFansNumberByUserIds($userIds);
		
		//推广员
		$promotionNumber = count($promotionIds);
		
		//库存
		$sotckNumber = AgentService::getStockNumberByAgentId($agentId);
		
		$commissionAmount = CommissionService::getCommissionGroupTypeByUserId($userId);
		
		//销售佣金
		$saleCommission = $commissionAmount[Commission::TYPE_SALE] ??0;
		
		//推荐佣金
		$recommendCommission = $commissionAmount[Commission::TYPE_RECOMMEND] ?? 0;
		
		//提货佣金
		$destoryCommission = $commissionAmount[Commission::TYPE_GOODS] ?? 0;
		
		//铺货佣金
		$stockCommission = $commissionAmount[Commission::TYPE_STOCK] ??0;
		
		//待签收数量
		$waitSignNumber = ShipOrderService::getWaitSignNumberByAgentId($agentId);
		
		//我的奖励数量
		$user = $this->auth->getUser();
		$selfNumber = $user['reward_number'];
		
		//场景店
		$shop = Db::name('agentShop')
			->where(['id' => ['in', $subAgentIds], 'status' => AgentShop::STATUS_Y, 'role' => User::LEVEL_SHOP])
			->count();
		
		//提货点
		$warehouse = Db::name('agentShop')
			->where(['id' => ['in', $subAgentIds], 'status' => AgentShop::STATUS_Y, 'role' => User::LEVEL_WAREHOUSE])
			->count();
		
		//未发货提醒数量
		$sendNumber = ShipOrderService::getSendTipsNumberByAgentId($agent['user_id']) ;
		
		//待发货订单数量
		$orderNumber = Db::name('order')
			->where(['center_id' => $agent['id'], 'status' => Order::STATUS_WAIT_SHIPPING, 'center_shipping' => 1])
			->count();
		
		$rewardNumber = Db::name('agentShop')
			->where(['role' => User::LEVEL_SHOP, 'recommend_user_id' => $userId, 'status' => AgentShop::STATUS_Y])
			->sum('gift_number');
		
		$this->success('查询成功', [
			'fans' => $fansNumber,
			'order_number' => $agent['order_number'],
			'sale_number' => $agent['sale_number'],
			'order_amount' => $agent['sale_amount'],
			'stock_order_number' => $waitSignNumber, //待签收发货记录
			'promotion' => $promotionNumber,
			'sale_commission' => $saleCommission + $recommendCommission,
			'goods_commission' => $destoryCommission,
			'stock_commission' => $stockCommission,
			'stock' => $sotckNumber,
			'self_number' => $agent['gift_number'],
			'reward_number' => $rewardNumber,
			'agent_info' => $agent,
			'is_admin' => $agent['user_id'] == $userId ? 0 : 1,
			'shop' => $shop,
			'warehouse' => $warehouse,
			'send_number' => $sendNumber,
			'user_id' => $agent['user_id'],
			'is_group' => $agent['is_group'],
			'destory_number' => $agent['destory_number'],
			'wait_send_order_number' => $orderNumber,
		]);
	}
	
	
	/**
	 * 我的订单
	 */
	public function order()
	{
		
		try {
			$agentId = ($this->agent)['id'];
			$page = $this->request->request('page', 1, 'int');
			$status = $this->request->request('status', Order::STATUS_WAIT_SHIPPING, 'int');
			$list = OrderService::getCenterOrder($agentId, $status, $page);
			$this->success('查询成功', $list);
		} catch (Exception $e) {
			$this->error($e->getMessage());
		}
	}
	
	/**
	 * 发货
	 */
	public function sendGoods()
	{
		$shippingName = $this->request->post('shipping_name');
		$shippingNo = $this->request->post('shipping_no');
		if(!$shippingName || !$shippingNo) {
			$this->error('快递名称和快递单号不能为空');
		}
		
		$orderId = $this->request->post('order_id');
		!$orderId && $this->error('参数错误');
		
		$agent = $this->agent;
		
		try {
			$res = OrderService::sendGoods($agent['id'], $orderId, $shippingName, $shippingNo, $agent['user_id']);
			$this->success('发货成功');
		} catch (Exception $e) {
			$this->error($e->getMessage());
		}
		
	}
}